const CLIENTID = $("#HiddenClientId").val(); const GAMENAME = $("#HiddenGameType").val(); const HUBURL = $('#HiddenNotificationUrl').val(); let notificationsArray = []; const connection = new signalR.HubConnectionBuilder() .withUrl(`${HUBURL}?clientId=${CLIENTID}&gameName=${GAMENAME}`) .build(); connection.on("RetrieveNotifications", (data) => { console.log(data); notificationsArray = notificationsArray.concat(data); notificationChecker(); }); async function start() { try { await connection.start(); console.log("SignalR Connected."); } catch (err) { //console.log(err); setTimeout(start, 5000); } }; // Start the connection. start(); // (async () => { // setInterval(() => { // retrieveNotification(); // }, 1000); // })(); let notificationsArrayIndex = 0; let prevNotificationsArrayIndexIndex = -1; function notificationChecker() { if (notificationsArrayIndex < notificationsArray.length) { if (prevNotificationsArrayIndexIndex !== notificationsArrayIndex) { retrieveNotification(notificationsArray[notificationsArrayIndex]); prevNotificationsArrayIndexIndex = notificationsArrayIndex; } } else { notificationsArrayIndex = 0; prevNotificationsArrayIndexIndex = -1; notificationsArray = []; } } function leaveGame() { let iframe = document.querySelector('#game-frame'); iframe.contentWindow.postMessage({ name: 'exit-game' }, "*"); } const retrieveNotification = (data) => { let display = true; let paresdDataText = JSON.parse(data.notificationText); if (paresdDataText && paresdDataText.Action == 'smartsoft_post.message.action') { gameEvent.errorMessage({ Code: paresdDataText.Code, Message: paresdDataText.Message }); if (paresdDataText.Display == false) { display = false; } } if (data.notificationText && display) { const popupMessageDiv = document.createElement("div"); const siblingtEl = document.getElementById("loader-div"); popupMessageDiv.setAttribute("id", "exeption-popup-message"); siblingtEl.parentNode.insertBefore( popupMessageDiv, siblingtEl.nextSibling ); let buttonsHtml = ""; // console.log(JSON.parse(data.Text)); if (paresdDataText.Buttons && paresdDataText.Buttons.Buttons) { for ( let index = 0; index < paresdDataText.Buttons.Buttons.length; index++ ) { const item = paresdDataText.Buttons.Buttons[index]; buttonsHtml += `
${item.Text}
`; } } popupMessageDiv.innerHTML = `

${GetCaption(paresdDataText.Title)}

${GetCaption(paresdDataText.Message)}
${buttonsHtml}
`; processNotification(data.notificationId); const buttons = document.querySelectorAll(".exeption-popup-button"); if (buttons) { for (let index = 0; index < buttons.length; index++) { buttons[index].addEventListener( "click", function () { popupMessageActionHandler( this.getAttribute("data-action"), this.getAttribute("data-redirect") ); }, false ); } } function popupMessageActionHandler(action, redirectUrl) { if (redirectUrl) { parent.location.href = redirectUrl; } else { if (action === "continue") { notificationsArrayIndex++; notificationChecker(); popupMessageDiv.remove(); } else if (action === "exit") { leaveGame(); } else { notificationsArrayIndex++; notificationChecker(); popupMessageDiv.remove(); } } } } else if (display == false) { processNotification(data.notificationId); setTimeout(() => { notificationsArrayIndex++; notificationChecker(); }, 500) } }; function processNotification(notificationId) { try { connection.invoke("PostProcess", CLIENTID, GAMENAME, notificationId); } catch (error) { setTimeout(() => { processNotification(notificationId) }, 2000) } }